package com.nefu.editor.web;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.common.ErrorMessage;
import com.nefu.editor.common.RestData;
import com.nefu.editor.common.util.TokenUtil;
import com.nefu.editor.core.model.Order;
import com.nefu.editor.core.model.User;
import com.nefu.editor.service.ArticleService;
import com.nefu.editor.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-14
 * @since : java 8
 */
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@RestController
public class WriterApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ArticleService articleService;
    private final OrderService orderService;

    public WriterApi(ArticleService articleService, OrderService orderService) {
        this.articleService = articleService;
        this.orderService = orderService;
    }

    @RequestMapping(value = "/works/{userSystemId}", method = RequestMethod.GET)
    public RestData getOrderList(@PathVariable(value = "userSystemId") Integer userSystemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                User user = new User();
                user.setSystemId(userSystemId);
                List<Map<String, Object>> articles = articleService.getArticleByWriterId(user);
                return new RestData(articles);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/orderList/{writerSystemId}", method = RequestMethod.GET)
    public RestData getWriterOrderList(@PathVariable(value = "writerSystemId") Integer writerSystemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                Order order = new Order();
                order.setWriterSystemId(writerSystemId);
                List<Map<String, Object>> orders = orderService.getOrderByWriterSystemId(order);
                return new RestData(orders);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/orderDetail/{systemId}", method = RequestMethod.GET)
    public RestData getWriterOrderDetail(@PathVariable(value = "systemId") Integer systemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                Order order = new Order();
                order.setSystemId(systemId);
                Map<String, Object> map = orderService.getOrderDetailByOrderSystemId(order);
                return new RestData(map);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/income/{writerSystemId}", method = RequestMethod.GET)
    public RestData getIncome(@PathVariable(value = "writerSystemId") Integer writerSystemId, HttpServletRequest request) {
        logger.info("getIncome  getIncome: "+writerSystemId);
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                Order order = new Order();
                order.setWriterSystemId(writerSystemId);
                Map<String, Object> map = orderService.getIncomeByWriterSystemId(order);
                return new RestData(map);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

}
